package com.czxy.demo04;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2020/2/1.
 */
public class Test23 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException, InvocationTargetException {
        List<Animal> getanimal = getanimal("dataAnimal.txt");
        for (Animal animal : getanimal) {
            System.out.println(animal);
        }
    }
    public static List<Animal>  getanimal(String  filename) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        List<Animal>  list=new ArrayList<>();
        List<String> strings = FileUtils.readLines(new File(filename), "UTF-8");
        for (String s : strings) {
            String[] split = s.split(" ");
            Class<?> aClass = Class.forName(split[0]);
            if (aClass.newInstance()instanceof Cat){
                Constructor<?> d = aClass.getDeclaredConstructor(String.class, String.class, String.class);
                list.add((Animal) d.newInstance(split[1].split("=")[1], split[2].split("=")[1], split[3].split("=")[1]));
            }else {
                Constructor<?> d = aClass.getDeclaredConstructor(String.class, String.class, int.class);
                list.add((Animal) d.newInstance(split[1].split("=")[1], split[2].split("=")[1], split[3].split("=")[1]));
            }

        }
        return list;
    }
}
